; NAME:
; WRITE_MOZART_NC
;
; PURPOSE:
; Writes a flux/emissions file in the correct format for MOZART
;
; CATEGORY:
; Write
;
; CALLING SEQUENCE:
; WRITE_MOZART_NC_AS, lat, lon, flux, date, time
;
; INPUTS:
; lat = Latitude
; lon = longitude
; flux = flux of dimensions lat x lon x time
; date = date of equal dimension to time
; time = time of equal dimension to date
; outname = what the file is to be called
; outdir = where you want the file to be saved
;
; OUTPUTS:
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;   Written Feb 19, 2014 by as13988
PRO  WRITE_MOZART_NC, lat, lon, flux, date, time, species, outname, outdir, VARIABLE_NAME = Variable_name, VARIABLE_long_name = Variable_long_name, TITLE = Title, UNITS = Units

  lon_ncname='lon'
  lat_ncname='lat'
  
  if keyword_set(Variable_Name) eq 0 then begin
    flux_ncname='emissions' 
    variable_long_name = Strcompress(strupcase(species) + " emission rate sum")
    endif else begin
      flux_ncname = Variable_Name
      if n_elements(variable_long_name) eq 0 then variable_long_name = Strcompress(strupcase(species) + " loss")
    endelse
 
  if keyword_set(units) eq 0 then units = "molecules/cm2/s"
  
  JD=mr_mozdate(date)
  
  ncname = outdir + outname
  
  
  ;Write it into a netcdf file
  ;Now write in MOZART netCDF file
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
  if keyword_set(title) eq 0 then Title=Species + 'emissions'
  author = 'Ann Stavert'
  
  fID=ncdf_create(ncName, /clobber)
  
  LonDID  = NCDF_DIMDEF(fID, lon_ncname, long(n_elements(lon)))
  LatDID  = NCDF_DIMDEF(fID, lat_ncname, long(n_elements(lat))) 
  TimeDID  = NCDF_DIMDEF(fID, 'time', /unlimited)
  
  ; Create a variable to hold the data
  lonID = NCDF_VARDEF(fId,lon_ncname, [LonDID], /DOUBLE)
  latID = NCDF_VARDEF(fId,lat_ncname, [LatDID], /DOUBLE)
  dateID = NCDF_VARDEF(fId,'date', [TimeDID], /LONG)
  timeID = NCDF_VARDEF(fId,'time', [TimeDID], /DOUBLE)
  qID = NCDF_VARDEF(fId, strjoin(flux_ncname), [LonDID, LatDID, TimeDID], /DOUBLE)
  
  ;Create some global attributes
  NCDF_ATTPUT, fid, /Global, "Title", strjoin(title)
  NCDF_ATTPUT, fid, /Global, "Author", author
  NCDF_ATTPUT, fid, /Global, "Created", systime()
  
  ; Create some attributes (about our variables)
  NCDF_ATTPUT, fId, lonId, "long_name", "longitude"
  NCDF_ATTPUT, fId, lonId, "units", "degrees_east"
  
  NCDF_ATTPUT, fId, latId, "long_name", "latitude"
  NCDF_ATTPUT, fId, latId, "units", "degrees_north"
  
  NCDF_ATTPUT, fId, qId, "long_name", variable_long_name[0]
  NCDF_ATTPUT, fId, qId, "units", units
  
  NCDF_ATTPUT, fId, TimeID, "long_name", "Time"
  NCDF_ATTPUT, fId, TimeID, "units", "days since 0000-01-01 00:00:00"
  
  NCDF_ATTPUT, fId, DateID, "long_name", "Date"
  NCDF_ATTPUT, fId, DateID, "units", "YYYYMMDD"
  
  ; Leave definition mode and enter data write mode
  NCDF_CONTROL, fId, /ENDEF
  
  ; Write the data
  NCDF_VARPUT, fId, lonId, lon
  NCDF_VARPUT, fId, LATID, lat
  
  NCDF_VARPUT, fId, dateID, date
  NCDF_VARPUT, fId, timeID, Time
  NCDF_VARPUT, fId, qId, flux
  
  ; Done
  NCDF_CLOSE, fId
  
  end
